import { formatXToC, formatYToR } from './lucky-sheet-util'

import config from './config'

const luckySheetConfig = {
  container: config.container,
  title: config.title,
  lang: 'zh',
  row: config.row,
  column: 64,
  data: [],
  hook: {},
  cellRightClickConfig: {
    deleteRow: false,
    deleteColumn: false,
    deleteCell: false,
    clear: false
  }
}

// THIS 创建序号参数数组
const createIndexParams = []

for (const [sheetIndex, sheetItem] of config.sheetList.entries()) {
  const operateCell = sheetItem.sheetForm.find(formItem => formItem.title === '操作')
  const indexCell = sheetItem.sheetForm.find(formItem => formItem.title === '序号')

  const sheetConfig = {
    name: sheetItem.sheetName,
    order: sheetIndex,
    column: 64,
    celldata: [],
    config: {
      merge: {},
      rowlen: {},
      columnlen: {},
      borderInfo: {}
    },
    frozen: {
      type: 'rangeBoth',
      range: { row_focus: 1, column_focus: operateCell.xSpan + indexCell.xSpan - 1 }
    },
    dataVerification: {}
  }

  let columnNum = 0
  for (const formItem of sheetItem.sheetForm) {
    if (formatXToC(formItem.x) + formItem.ySpan > columnNum) {
      columnNum = formatXToC(formItem.x) + formItem.ySpan
    }

    const cellConfig = {
      r: formatYToR(formItem.y),
      c: formatXToC(formItem.x),
      v: {
        ct: {
          fa: 'General',
          t: 'g'
        },
        v: formItem.title,
        fc: formItem.textColor,
        bg: formItem.color,
        vt: 0,
        ht: 0,
        tb: 0
      }
    }
    sheetConfig.celldata.push(cellConfig)

    if (formItem.xSpan > 1 || formItem.ySpan > 1) {
      sheetConfig.config.merge[formatYToR(formItem.y) + '_' + formatXToC(formItem.x)] = {
        r: formatYToR(formItem.y),
        c: formatXToC(formItem.x),
        rs: formItem.ySpan,
        cs: formItem.xSpan
      }
    }

    if (formItem.width !== undefined) {
      if (formItem.width === 'auto') {
        sheetConfig.config.columnlen[`${formatXToC(formItem.x)}`] = formItem.title.length * 18 > parseInt(config.widthMin) ? formItem.title.length * 18 : parseInt(config.widthMin)
      } else {
        sheetConfig.config.columnlen[`${formatXToC(formItem.x)}`] = parseInt(formItem.width) > parseInt(config.widthMin) ? parseInt(formItem.width) : parseInt(config.widthMin)
      }
    }

    if (formItem.options !== undefined) {
      if (Array.isArray(formItem.options)) {
        for (let index = 2; index < 1000; index++) {
          sheetConfig.dataVerification[index + '_' + formatXToC(formItem.x)] = {
            type: 'dropdown',
            value1: formItem.options.join(',')
          }
        }
      }
    }
  }

  sheetConfig.column = columnNum
  luckySheetConfig.data.push(sheetConfig)

  createIndexParams.push({ order: sheetIndex, operateCell: operateCell, indexCell: indexCell, columnNum: columnNum })
}

luckySheetConfig.hook = {
  cellUpdated: (r, c, oldValue, newValue, isRefresh) => {
    const currentSheet = luckysheet.getSheet()
    const indexParam = createIndexParams.find(param => param.order === currentSheet.order)
    const rowValueList = []
    if (r > indexParam.indexCell.ySpan - 1 && c > indexParam.operateCell.xSpan + indexParam.indexCell.xSpan - 1) {
      for (let i = indexParam.operateCell.xSpan + indexParam.indexCell.xSpan; i < indexParam.columnNum; i++) {
        rowValueList.push(luckysheet.getCellValue(r, i, { order: currentSheet.order }))
      }
      if (Array.from(new Set(rowValueList)).length > 1) {
        luckysheet.setCellValue(r, indexParam.operateCell.xSpan, {
          ct: {
            fa: 'General',
            t: 'g'
          },
          v: r - indexParam.indexCell.ySpan + 1,
          fc: indexParam.indexCell.config.textColor,
          bg: indexParam.indexCell.config.color,
          vt: 0,
          ht: 0,
          tb: 0
        }, { order: currentSheet.order })
      } else {
        luckysheet.setCellValue(r, indexParam.operateCell.xSpan, {
          ct: {
            fa: 'General',
            t: 'g'
          },
          v: null,
          fc: '#ffffff',
          bg: '#ffffff',
          vt: 0,
          ht: 0,
          tb: 0
        }, { order: currentSheet.order })
      }
    }
  }
}

export default luckySheetConfig
